/*
 * synth.h
 * This file is part of lab10
 *
 * Copyright (C) 2012 - Luke Westby & Jon Gaustch
 *
 * lab10 is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * lab10 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with lab10. If not, see <http://www.gnu.org/licenses/>.
 */

// libc includes
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <math.h>
#include <string.h>
#include <errno.h>

// linux system includes
#include <fcntl.h>
#include <unistd.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

// pulseaudio includes
#include <pulse/simple.h>
#include <pulse/error.h>

// gui includes
#include "gfx.h"
#include "font.h"

// window size
#define WINDOW_X 600
#define WINDOW_Y 600

// buffer to be read to the soundcard
#define BUFSIZE 64

// wavetables
#define WAVESIZE 4096

// frequencies
#define NNOTES 24

// sample rate
#define SAMPLERATE 44100

// error macros - didn't end up really using these but who knows
#define SYNTH_SUCCESS 0
#define SYNTH_MEM_ERROR -1

/* initialization functions ------------------------------------------------- */
int init_wavetables(float *saw, float *sqr, float *sin);
int init_freqtable(double *freqs);
pa_simple *init_soundcard();

/* synthesis functions ------------------------------------------------------ */
void process(char key, float *wave_1, float *wave_2, double *freqs, float sep,
			 float vol, pa_simple *s);
void drain(pa_simple *s);

/* gui functions ------------------------------------------------------------ */
void draw_piano_roll(double xOrigin, double yOrigin);
void draw_white_keys(double xOrigin, double yOrigin, int key_width, int key_height, int num_keys);
void draw_black_keys(double xOrigin, double yOrigin, int key_width, int key_height, int num_keys);
void draw_waveform_boxes(int x, int y);
void draw_waveforms(int x, int y, int box_length, int box_height);
void label_keys(double xOrigin, double yOrigin, int key_width, int key_height, int num_keys);
void draw_wave_selection_numbers(double xOrigin, double yOrigin, int size);
void draw_one(int x, int y, int size);
void draw_two(int x, int y, int size);
void draw_three(int x, int y, int size);
void draw_curve(int x, int y, int r, double alpha, double beta);

/* memory cleanup ----------------------------------------------------------- */
void cleanup(float *saw, float *sqr, float *sine, double *freqs, pa_simple *s);
